//
//  LBCustomNavImageView.m
//  jslj
//
//  Created by 杨庆人 on 2017/10/9.
//  Copyright © 2017年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBCustomNavImageView.h"
#import <Masonry/Masonry.h>
#import <LBCategory/LBCategory.h>

@interface LBCustomNavImageView ()
@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong)UILabel *titleLabel;
@end

@implementation LBCustomNavImageView

- (instancetype)initWithTitle:(NSString *)title bgImage:(UIImage *)bgImage {
    return [self initWithTitle:title bgImage:bgImage rightItem:nil];
}
- (instancetype)initWithTitle:(NSString *)title bgImage:(UIImage *)bgImage rightItem:(__kindof UIView *)rightItem {
    if (self = [super init]) {
    
        self.imageView.image = bgImage;
        [self addSubview:self.imageView];
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
        
        [self addSubview:self.lbNavBar];
        [self.lbNavBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(self);
            make.height.mas_equalTo(customNav_H);  // 导航栏高度 + 状态栏高度
        }];
        
        [self.lbNavBar addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.lbNavBar.mas_centerY).offset([[UIApplication sharedApplication] statusBarFrame].size.height / 2.0);
            make.left.right.mas_equalTo(self.lbNavBar);
            make.height.mas_equalTo(44);
        }];
        self.titleLabel.text = title;
        
        [self.lbNavBar addSubview:self.backButton];
        [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(55, 44));
            make.centerY.mas_equalTo(self.titleLabel.mas_centerY);
            make.left.offset(5);
        }];
        
        if (rightItem) {
            [self.lbNavBar addSubview:rightItem];
            [rightItem mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.titleLabel.mas_centerY);
                make.right.offset(-20);
            }];
        }
    }
    return self;
}

- (void)turnBack {
    [[UIApplication getCurrentViewController].navigationController popViewControllerAnimated:YES];
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.userInteractionEnabled = YES;
    }
    return _imageView;
}

- (UIView *)lbNavBar {
    if (!_lbNavBar) {
        _lbNavBar = [[UIView alloc] init];
    }
    return _lbNavBar;
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton setImage:[UIImage imageNamed:@"LBCustomNavigationBar.bundle/back"] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(turnBack) forControlEvents:UIControlEventTouchUpInside];
        [_backButton setImageEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];
    }
    return _backButton;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:18];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (void)setCustomNavTitle:(NSString *)title {
    self.titleLabel.text = title;
}



@end
